#include "Tests.h"
#include "Source.h"
#include "Sink.h"
#include "Processor.h"
#include "OnlineStatistic.h"
#include <iostream>
#include <fstream>

using namespace std;

long Test(ISource<float>& source, const char* outFile) {
	ofstream out(outFile);
	Printer<float> sink(out);
	SimpleProcessor<float, float> processor;
	processor.Run(&source, NULL, &sink);
	out.close();
	return sink.ProcessedCount();
}

long TestProcessor() {
	RandomSource source(100000, 10);
	return Test(source, "TestProcessor_Out.txt");
}

long TestDataReader() {
	const char* inFile = "vidata.dat";
	FILE* pFile = NULL;
	if (!(pFile = fopen(inFile, "r"))) {
		cout << "Missing test input file \"vidata.dat\"." << endl;
		return 0;
    }
	else
		fclose(pFile);
	ifstream in("vidata.dat", ios::binary);
	VIReader source(in, VIReader::mode_t::Current);
	return Test(source, "TestDataReader_Out.txt");
}

long TestOnlineMedian() {
	int nValues = 20;
	int winSize = 6;
	ofstream out("TestOnlineMedian_Out.txt");
	OnlineMedian<int, float> mNoFill(winSize, false);
	for(int i=0; i < nValues; i++) {
		mNoFill.RecordInput(rand());
		mNoFill.PrintState(out);
	}
	out << endl;
	OnlineMedian<int, float> mFill(winSize, true, 0);
	for(int i=0; i < nValues; i++) {
		mFill.RecordInput(rand());
		mFill.PrintState(out);
	}
	return nValues*2;
}